sdkRoot ?= ./

include ${sdkRoot}/setting.mk


######################################
# soc
######################################
SOC ?= link32fa016bx

include  ${sdkRoot}/bsp/soc/${SOC}/setting.mk

######################################
# target
######################################
TARGET ?= link32

#######################################
# paths
#######################################
# Build path
BUILD_DIR ?= build

######################################
# building variables
######################################
# debug build?
DEBUG ?= 1

# optimization
OPT ?= -O3


######################################
# source
######################################
# C sources
C_SOURCES := \
${C_SOURCES} \
${sdkRoot}/bsp/libwrap/syscalls.c \
${sdkRoot}/bsp/libwrap/printf.c \
${sdkRoot}/bsp/libwrap/sysmem.c

# ASM sources
ASM_SOURCES := \
${ASM_SOURCES}

#######################################
# CFLAGS
#######################################

# mcu
MCU = $(ARCH) $(CPU)

# macros for gcc
# AS defines
AS_DEFS := ${AS_DEFS}

# C defines
C_DEFS := ${C_DEFS}

# AS includes
AS_INCLUDES := ${AS_INCLUDES}

# C includes
C_INCLUDES :=  \
${C_INCLUDES} \
-I ${sdkRoot}/bsp/XMSIS/Core/Include


# compile gcc flags
ASFLAGS = $(MCU) $(AS_DEFS) $(AS_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections -nostartfiles --specs=nosys.specs

CFLAGS = $(MCU) $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections -nostartfiles --specs=nosys.specs -fno-exceptions

ifeq ($(DEBUG), 1)
CFLAGS += -g -gdwarf-2
endif


# Generate dependency information
CFLAGS += -MMD -MP -MF"$(@:%.o=%.d)"


#######################################
# LDFLAGS
#######################################
# link script
ifeq ( $(BootFromSRAM), 1 )
LDSCRIPT = ${sdkRoot}/bsp/soc/${SOC}/gcc/${SOC}_flash.ld
else
LDSCRIPT = ${sdkRoot}/bsp/soc/${SOC}/gcc/${SOC}_flash.ld
endif

# libraries
LIBS = -lc -lm
LIBDIR = 
LDFLAGS = $(MCU) -T$(LDSCRIPT) $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-sections -nostartfiles --specs=nosys.specs

# default action: build all
all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin $(BUILD_DIR)/$(TARGET).asm $(BUILD_DIR)/$(TARGET).inc


#######################################
# build the application
#######################################
# list of objects
OBJECTS = $(addprefix $(BUILD_DIR)/,$(notdir $(C_SOURCES:.c=.o)))
vpath %.c $(sort $(dir $(C_SOURCES)))
# list of ASM program objects
OBJECTS += $(addprefix $(BUILD_DIR)/,$(notdir $(ASM_SOURCES:.S=.o)))
vpath %.S $(sort $(dir $(ASM_SOURCES)))

$(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR) 
	$(CC) -c $(CFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@

$(BUILD_DIR)/%.o: %.S Makefile | $(BUILD_DIR)
	$(AS) -c $(CFLAGS) $< -o $@

$(BUILD_DIR)/$(TARGET).elf: $(OBJECTS) Makefile
	$(CC) $(OBJECTS) $(LDFLAGS) -o $@
	$(SZ) $@

$(BUILD_DIR)/%.hex: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
	$(HEX) $< $@
	
$(BUILD_DIR)/%.bin: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
	$(BIN) $< $@	
$(BUILD_DIR)/%.asm: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
	$(DM) -d $<  > $@

$(BUILD_DIR)/%.inc: $(BUILD_DIR)/%.elf | $(BUILD_DIR)
	$(DM) -D $<  > $@
	
$(BUILD_DIR):
	mkdir $@		


GDB_HOST ?= ${TargetHost}
GDB_PORT ?= 3333
GDB_CONNECT_CMDS += -ex 'target extended-remote ${GDB_HOST}:${GDB_PORT}'

GDB_UPLOAD_ARGS ?= --batch
GDB_UPLOAD_CMDS += -ex 'set remotetimeout 240'
GDB_UPLOAD_CMDS += ${GDB_CONNECT_CMDS}
GDB_UPLOAD_CMDS += -ex 'monitor reset halt'
GDB_UPLOAD_CMDS += -ex 'load'
GDB_UPLOAD_CMDS += -ex 'monitor resume'
GDB_UPLOAD_CMDS += -ex 'quit'


upload:
	${GDB} ${BUILD_DIR}/$(TARGET).elf $(GDB_UPLOAD_ARGS) $(GDB_UPLOAD_CMDS)
	
gdb:
	${GDB} ${BUILD_DIR}/$(TARGET).elf ${GDB_CONNECT_CMDS} -ex 'load'
	



#######################################
# clean up
#######################################
clean:
	-rm -rf $(BUILD_DIR)
#######################################
# dependencies
#######################################
-include $(wildcard $(BUILD_DIR)/*.d)



# *** EOF ***
